package com.xinli.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinli.business.domain.FtMember;
import com.xinli.business.domain.LfNotice;
import com.xinli.business.service.IFtMemberService;
import com.xinli.business.service.ILfNoticeService;
import com.xinli.common.utils.DateUtils;
import com.xinli.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.xinli.system.domain.SysNotice;
import com.xinli.system.mapper.SysNoticeMapper;
import com.xinli.system.service.ISysNoticeService;
import org.springframework.util.CollectionUtils;

/**
 * 公告 服务层实现
 *
 * @author ruoyi
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService
{
    @Autowired
    private SysNoticeMapper noticeMapper;

    @Autowired
    private ILfNoticeService lfNoticeService;

    @Autowired
    private IFtMemberService ftMemberService;

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId)
    {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice)
    {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice)
    {
         noticeMapper.insertNotice(notice);
         // 异步发布消息方法
           asyncPublishNotice(notice);
         return 1;
    }

    @Async
    public void asyncPublishNotice(SysNotice notice) {
        // 执行异步操作，如推送消息、记录日志等

        ArrayList<LfNotice> lfNoticesList = new ArrayList<>();
        QueryWrapper<FtMember>  ftMember =new  QueryWrapper<FtMember>();
        List<FtMember> ftMembers = ftMemberService.list(ftMember);
        for (FtMember member : ftMembers) {
            LfNotice  lfNotice =new LfNotice();
            lfNotice.setSenderId(SecurityUtils.getUserId());
            lfNotice.setReceiverId(member.getMemberId());
            lfNotice.setContentType("1");
            lfNotice.setType("1");
            lfNotice.setInformation(notice.getNoticeContent());
            lfNotice.setCreateTime(DateUtils.getNowDate());
            lfNoticesList.add(lfNotice);
        }
        if(!CollectionUtils.isEmpty(lfNoticesList)){
            lfNoticeService.saveBatch(lfNoticesList);
        }

    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice)
    {
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId)
    {
        return noticeMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds)
    {
        return noticeMapper.deleteNoticeByIds(noticeIds);
    }
}
